package com.spdg.ring.sqlite.dao.impl;

import java.util.List;

import android.content.Context;
import cn.wolf.sqlite.dao.impl.BaseDaoImpl;
import cn.wolf.tools.Log;

import com.spdg.ring.sqlite.DBHelper;
import com.spdg.ring.sqlite.dao.ArticleDao;
import com.spdg.ring.sqlite.model.Article;

/**
 * <p>Title: 文章详细页面Dao实现         </p>
 * <p>Description: 
 * </p>
 * <p>@author: Eric.wsd                </p>
 * <p>Copyright: Copyright (c) 2012    </p>
 * <p>Company: HX Co., Ltd.          </p>
 * <p>Create Time: 2012-11-30             </p>
 * <p>Update Time:                     </p>
 * <p>Updater:                         </p>
 * <p>Update Comments:                 </p>
 */
public class ArticleDaoImpl extends BaseDaoImpl<Article> implements ArticleDao {
	private static ArticleDaoImpl articleDao;

	public ArticleDaoImpl(Context context) {
		super(new DBHelper(context));
	}

	public static ArticleDaoImpl newInstance(Context context) {
		if (articleDao == null) {
			articleDao = new ArticleDaoImpl(context);
		}

		return articleDao;
	}

	@Override
	public boolean isExist(Article article) {
		String sql = "select * from t_article where file = ?";
		return articleDao.isExist(sql, new String[] { article.getFile() });
	}

	@Override
	public void delete(Article article) {
		String sql = "delete from t_article where file = ?";
		articleDao.execSql(sql, new String[] { article.getFile() });
	}

	@Override
	public Article findByKey(String fileKey) {
		String sql = "select * from t_article where file = ?";

		List<Article> list = articleDao.rawQuery(sql, new String[] { fileKey });

		if (list != null && list.size() > 0) {
			return list.get(0);
		}

		return null;
	}

	@Override
	public void update(Article article) {
		if (article == null) {
			Log.e("Article is null");
			return;
		}

		Article entity = findByKey(article.getFile());
		if (entity != null) {
			article.setId(entity.getId());
		}

		super.update(article);

	}

}
